package com.bandit.service.impl;

import com.bandit.dao.BlogDao;
import com.bandit.pojo.Blog;
import com.bandit.pojo.BlogDate;
import com.bandit.pojo.Page;
import com.bandit.service.BlogService;
import com.bandit.utils.MyConstants;
import com.bandit.utils.PageSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class BlogServiceImpl implements BlogService {

    @Autowired
    BlogDao blogDao;

    @Override
    public List<Blog> getAllBlog() {
        return blogDao.getAllBlog();
    }

    @Override
    public List<Blog> getSix() {
        return blogDao.getSix();
    }

    @Override
    public List<Blog> getShowBlog() {
        return blogDao.getShowBlog();
    }

    @Override
    public List<Integer> searchBlog(String str) {
        return blogDao.searchBlog(str);
    }

    @Override
    public Blog getBlogById(int id) {
        return blogDao.getBlogById(id);
    }

    @Override
    public List<Blog> getBlogByTypeId(int blogTypeId) {
        return blogDao.getBlogByTypeId(blogTypeId);
    }

    @Override
    public Page<Blog> getBlogByPage(int currentPage) {
        HashMap<String, Object> map = new HashMap<>();
        Page<Blog> page = new Page<>();

        // 当前页数
        page.setCurrentPage(currentPage);

        // 每页显示条数
        int pageSize = MyConstants.PAGE_SIZE;
        page.setPageSize(pageSize);

        // 记录总数
        int sum = blogDao.selectCount();
        page.setSum(sum);

        // 总页数
        double decimal = sum;
        Double num = Math.ceil(decimal / pageSize);
        page.setTotalPage(num.intValue());

        // 设置开始结束页数
        map.put("start", (currentPage - 1) * pageSize);
        map.put("size", page.getPageSize());

        // 每页显示的数据
        List<Blog> blogList = blogDao.getBlogByPage(map);
        page.setBlogList(blogList);

        return page;
    }

    @Override
    public int selectCount() {
        return blogDao.selectCount();
    }

    @Override
    public int getLastBlogId() {
        return blogDao.getLastBlogId();
    }

    @Override
    public void insertBlog(Blog blog) {
        blogDao.insertBlog(blog);
    }

    @Override
    public void deleteBlogById(int id) {
        blogDao.deleteBlogById(id);
        blogDao.deleteCommentByBlogId(id);
    }

    @Override
    public void updateBlog(Blog blog) {
        blogDao.updateBlog(blog);
    }

    @Override
    public List<BlogDate> getYearAndMonth() {
        return blogDao.getYearAndMonth();
    }

    @Override
    public List<Blog> getBlogByDate(BlogDate blogDate) {
        return blogDao.getBlogByDate(blogDate);
    }

    @Override
    public int getClickHit(int id) {
        return blogDao.getClickHit(id);
    }

    @Override
    public void updateClickHit(int id) {
        blogDao.updateClickHit(id);
    }
}
